package pannelli.main;

import it.jack.PannelloPrincipale;

import java.awt.Dimension;
import java.awt.Point;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import com.cloudgarden.layout.AnchorConstraint;
import com.cloudgarden.layout.AnchorLayout;
import it.jack.Main;
import java.beans.PropertyVetoException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JButton;

import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.JOptionPane;
import javax.swing.WindowConstants;
import javax.swing.SwingUtilities;
import javax.swing.event.InternalFrameEvent;
import javax.swing.event.InternalFrameListener;

import pannelli.AggiungiAccessorioInMagazzino;
import pannelli.sub.SubPannelloAccessoriInMagazzino;

/**
 * This code was edited or generated using CloudGarden's Jigloo SWT/Swing GUI
 * Builder, which is free for non-commercial use. If Jigloo is being used
 * commercially (ie, by a corporation, company or business for any purpose
 * whatever) then you should purchase a license for each developer using Jigloo.
 * Please visit www.cloudgarden.com for details. Use of Jigloo implies
 * acceptance of these licensing terms. A COMMERCIAL LICENSE HAS NOT BEEN
 * PURCHASED FOR THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED LEGALLY FOR
 * ANY CORPORATE OR COMMERCIAL PURPOSE.
 */
public class PannelloMagazzino extends JInternalFrame implements InternalFrameListener {

    private static final long serialVersionUID = 1L;
    private JButton AggiungiAccessorio;
    private JButton RimuoviAccessorio;
    private SubPannelloAccessoriInMagazzino subPannelloAccessoriInMagazzino1;
    private JButton RicercaAccessorio;
    private static PannelloMagazzino istance;

    public static PannelloMagazzino getIstance() {
        return istance;
    }

    public void AbilitaRimozione(boolean valore) {
        RimuoviAccessorio.setEnabled(true);
    }

    public void centra() {
        Dimension dimension = Toolkit.getDefaultToolkit().getScreenSize();
        setLocation(new Point((dimension.width - getSize().width) / 2,
                (dimension.height - getSize().height) / 2));

    }

    public PannelloMagazzino() {
        super("Pannello Magazzino", true, true, true, true);
        this.istance = this;
        initGUI();
        centra();
        buttonAction();
        setVisible(true);
        this.addInternalFrameListener(this);
    }

    private void initGUI() {
        try {
            AnchorLayout thisLayout = new AnchorLayout();
            getContentPane().setLayout(thisLayout);
            {
                getContentPane().add(SubPannelloAccessoriInMagazzino.getInstance(), new AnchorConstraint(1, 656, 1001, 3, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));
                SubPannelloAccessoriInMagazzino.getInstance().setPreferredSize(new java.awt.Dimension(440, 433));
            }
            {
                RicercaAccessorio = new JButton();
                getContentPane().add(RicercaAccessorio, new AnchorConstraint(386, 962, 451, 674, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));
                RicercaAccessorio.setText("Ricerca Accessorio");
                RicercaAccessorio.setPreferredSize(new java.awt.Dimension(194, 28));
            }
            setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
            {
                RimuoviAccessorio = new JButton();
                RimuoviAccessorio.setEnabled(false);
                getContentPane().add(RimuoviAccessorio, new AnchorConstraint(236, 962, 301, 671, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));
                RimuoviAccessorio.setText("Rimuovi accessorio dal magazzino");
                RimuoviAccessorio.setPreferredSize(new java.awt.Dimension(196, 28));
                RimuoviAccessorio.setEnabled(Main.getInstance().getUser().getUsername().equals("admin"));
            }
            {
                AggiungiAccessorio = new JButton();
                getContentPane().add(AggiungiAccessorio, new AnchorConstraint(84, 962, 151, 674, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));
                AggiungiAccessorio.setText("Aggiungi accessorio in magazzino");
                AggiungiAccessorio.setPreferredSize(new java.awt.Dimension(194, 29));
            }
            pack();
            this.setSize(690, 471);
        } catch (Exception e) {
            //add your error handling code here
            e.printStackTrace();
        }
    }

    private void buttonAction() {
        RicercaAccessorio.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent arg0) {
                PannelloRicerca pannelloRicerca = new PannelloRicerca(2);
                try {
                    pannelloRicerca.setSelected(true);
                } catch (PropertyVetoException ex) {
                    Logger.getLogger(PannelloMagazzino.class.getName()).log(Level.SEVERE, null, ex);
                }
                Main.getInstance().addInDesktop(pannelloRicerca);

            }
        });

        AggiungiAccessorio.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent arg0) {
                new AggiungiAccessorioInMagazzino();
                AbilitaRimozione(false);
            }
        });

        RimuoviAccessorio.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent arg0) {
                if (SubPannelloAccessoriInMagazzino.getInstance().removeAccessorio()) {
                    AbilitaRimozione(false);
                    JOptionPane.showMessageDialog(null, "Accessorio Rimosso!");
                    SubPannelloAccessoriInMagazzino.getInstance().initTable();
                } else {
                    JOptionPane.showMessageDialog(null, "Si e' verificato un errore col database!");
                }
            }
        });

    }

    public void update() {
        subPannelloAccessoriInMagazzino1.initTable();
    }

    @Override
    public void internalFrameActivated(InternalFrameEvent arg0) {
        PannelloPrincipale.getIstance().disableButtons();
    }

    @Override
    public void internalFrameClosed(InternalFrameEvent arg0) {
        // TODO Auto-generated method stub
    }

    @Override
    public void internalFrameClosing(InternalFrameEvent arg0) {
        PannelloPrincipale.getIstance().activateButtons();
        this.dispose();
    }

    @Override
    public void internalFrameDeactivated(InternalFrameEvent arg0) {
        // TODO Auto-generated method stub
    }

    @Override
    public void internalFrameDeiconified(InternalFrameEvent arg0) {
        // TODO Auto-generated method stub
    }

    @Override
    public void internalFrameIconified(InternalFrameEvent arg0) {
        // TODO Auto-generated method stub
    }

    @Override
    public void internalFrameOpened(InternalFrameEvent arg0) {
        PannelloPrincipale.getIstance().disableButtons();
    }
}
